
//Faculty of Computer Science and Engineering
//Ho Chi Minh City University of Technology
//Initial code for Assignment 3 - Programming Fundamentals 501127 - Spring 2009
//Author: Nguyen Ho Man Rang
//#ifndef _musketeer_cpp_
//#include "musketeer.cpp"
//#define _musketeer_cpp_
//#endif

#ifndef _definition_h_
#include "defs.h"
#define _definition_h_
#endif

int coun = 0;
struct musketeer M[4];  // M[0] : d'Artagnan
						// M[1] : Athos
						// M[2] : Porthos
						// M[3] : Aramis


const int EVENT_SIZE = 1001;

//Create a new crystal
void newCrystal(Crystal*& pointer, int num, int level)
{
	if(num==0){
		pointer = new Crystal;
		pointer->level = level;
		pointer->nextCrystal = NULL;
	}
	else{
		Crystal* tail = pointer;
		for(int i=1;i<num;i++){
			tail = tail->nextCrystal;
		}
		tail->nextCrystal = new Crystal;
		tail->nextCrystal->level=level;
		tail->nextCrystal->nextCrystal=NULL;
	}
	coun =coun+1;
}
//Delete a crystal
void deleteCrystal(Crystal* pointer)
{
	delete(pointer);
	coun--;
}

//read data from input file to corresponding variables
//return 1 if successfully done, otherwise return 0
int readFile (int D [], int A [], int P [], int R [],int& I, int E [], int& nEvent) {
	ifstream fin("input.txt");
	int i = 0;
	int temp = 0;

	if (!fin) {
		return 0;
	}
	
	//Read D
	for (i = 0; i < 3; i++) {
		fin>>D[i];
	}

	//Read A
	for (i = 0; i < 3; i++) {
		fin>>A[i];
	}

	//Read P 
	for (i = 0; i < 3; i++) {
		fin>>P[i];
	}

	//Read R
	for (i = 0; i < 3; i++) {
		fin>>R[i];
	}

	//Read I
	fin>>I;

	//Read E and nEvent
	nEvent = 0;
	while (!fin.eof()) {
		temp = 2000;
		fin>>temp;
		if (temp < 1000 && temp > -1000) {
			E[nEvent] = temp;
			nEvent++;
		}
	}
	
	fin.close();
	return 1;
}

int sum(Crystal* crystal, int num){
	Crystal* tail = crystal;
	int sum = 0;
	for(int i=0;i<num;i++){
		sum = sum + tail->level;
		tail = tail->nextCrystal;
	}
	return sum;
}

/*	Display the output on screen
 *	Return value: 
 *		Void
 */
void display () {
	//output the values of the musketeers
	for (int i = 0; i < 4;i++) {
		cout<<M[i].HP<<"\t";

		if (M[i].pC1 == NULL) {
			cout<<0<<" ";
		} else {
			cout<<sum(M[i].pC1,M[i].npC1)<<" ";
		}
		
		if (M[i].pC2 == NULL) {
			cout<<0<<" ";
		} else {
			cout<<sum(M[i].pC2,M[i].npC2)<<" ";
		}

		if (M[i].pC3 == NULL) {
			cout<<0<<" ";
		} else {
			cout<<sum(M[i].pC3,M[i].npC3);
		}
		
		cout<<endl;
	}

}

void collectGarbage () {
	for(int i = 0; i < 4; i++ ) {
		
		// Huy bo Sapphire
		if (M[i].pC1 != NULL) { //co crystal
			Crystal* share = M[i].pC1;
			for(int k=0;k<M[i].npC1;k++){
				//Tim xem co ai dung chung voi M[i] khong, neu co gan pC1 cua nguoi do la pC1->nextCrystal;
				for (int j = i + 1; j < 4; j++) {
					if (M[j].pC1 == share) {
						M[j].pC1 = M[j].pC1->nextCrystal;
						M[j].npC1--;
					}
				}
				Crystal* temp = share;
				share = share->nextCrystal;
				deleteCrystal(temp);
			}
		}							
		M[i].pC1 = NULL;
		M[i].npC1 = 0;
		//Huy bo Emerald
		if (M[i].pC2 != NULL) { //co crystal
			Crystal* share = M[i].pC2;
			for(int k=0;k<M[i].npC2;k++){
				//Tim xem co ai dung chung voi M[i] khong, neu co gan pC2 cua nguoi do la pC2->nextCrystal;
				for (int j = i + 1; j < 4; j++) {
					if (M[j].pC2 == share) {
						M[j].pC2 = M[j].pC2->nextCrystal;
						M[j].npC2--;
					}
				}
				Crystal* temp = share;
				share = share->nextCrystal;
				deleteCrystal(temp);
			}
		}							
		M[i].pC2 = NULL;
		M[i].npC2 = 0;
		//Huy bo Aquamarine
		if (M[i].pC3 != NULL) { //co crystal
			Crystal* share = M[i].pC3;
			for(int k=0;k<M[i].npC3;k++){
				//Tim xem co ai dung chung voi M[i] khong, neu co gan pC3 cua nguoi do la pC3->nextCrystal;
				for (int j = i + 1; j < 4; j++) {
					if (M[j].pC3 == share) {
						M[j].pC3 = M[j].pC3->nextCrystal;
						M[j].npC3--;
					}
				}
				Crystal* temp = share;
				share = share->nextCrystal;
				deleteCrystal(temp);
			}
		}							
		M[i].pC3 = NULL;
		M[i].npC3 = 0;
	}
}
int main(void) {
	int D[3];
	int A[3];
	int P[3];
	int R[3];
	int E[1000];
	int I;
	int nEvent;
	if(!readFile(D, A, P, R, I, E, nEvent)) {
		return 0;
	}

	init(D, A, P, R, M);
	battle(M, I, E, nEvent);
	display();
	collectGarbage();
	cout<<coun;
	cout<<endl;
	system("PAUSE");
	return 0;
}
